package com.zk.demo.service.impl;

import com.zk.demo.bean.Fund;
import com.zk.demo.mapper.FundMapper;
import com.zk.demo.service.FundService;
import com.zk.demo.util.GetMoney;
import com.zk.demo.util.NubertoString;
import com.zk.demo.util.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class FundServiceImpl implements FundService {

    @Autowired
    private FundMapper fundMapper;


    @Override
    public List<Fund> selectAll(Map<String,Integer> param) {
        List<Fund> funds = fundMapper.selectAll(param);
        funds.stream().forEach((fund)->{
            //当日盈亏率
            BigDecimal plrate = fund.getPlrate();
            //期望获得的金额
            BigDecimal expectMoney = fund.getExpectMoney();
            //本金
            BigDecimal principal = fund.getPrincipal();

            //已亏损/盈利金额
            BigDecimal loseMoney = principal.multiply(plrate);
            fund.setLoseMoney(loseMoney);

            Fund getFund = GetMoney.getMoney(principal, plrate, expectMoney);
            //补仓金额
            BigDecimal money = getFund.getRepairMoney();
            //第二天卖出手续费
            fund.setTomorrMoney(getFund.getTomorrMoney());
            //亏损率
            fund.setPlrateStr(NubertoString.getText(plrate));
            fund.setRepairMoney(money);
        });

        return funds;
    }

    @Override
    public List<Fund> selectAll() {
        List<Fund> funds = fundMapper.selectAll(null);
        funds.stream().forEach((fund)->{
            //当日盈亏率
            BigDecimal plrate = fund.getPlrate();
            //期望获得的金额
            BigDecimal expectMoney = fund.getExpectMoney();
            //本金
            BigDecimal principal = fund.getPrincipal();

            //已亏损/盈利金额
            BigDecimal loseMoney = principal.multiply(plrate);
            fund.setLoseMoney(loseMoney);

            Fund getFund = GetMoney.getMoney(principal, plrate, expectMoney);
            //补仓金额
            BigDecimal money = getFund.getRepairMoney();
            //第二天卖出手续费
            fund.setTomorrMoney(getFund.getTomorrMoney());
            //亏损率
            fund.setPlrateStr(NubertoString.getText(plrate));
            fund.setRepairMoney(money);
        });

        return funds;
    }

    @Override
    public ResultResponse add(Fund fund) {
        try {
            fundMapper.add(fund);
        } catch (Exception e) {
            e.getStackTrace();
            return  ResultResponse.buildFailed();
        }
        return ResultResponse.buildSuccess();
    }

    @Override
    public ResultResponse del(int id) {
        try {
            fundMapper.del(id);
        } catch (Exception e) {
            e.getStackTrace();
            return  ResultResponse.buildFailed();
        }
        return ResultResponse.buildSuccess();
    }
}
